#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

using FighterChessEngine;
using FighterChessEngine.Enum;
using FighterChessEngine.FC_Fighter;
using FighterChessEngine.FC_Fighter.Fighters;
using FighterChessEngine.FC_Board;
using FighterChessEngine.FC_Player;

using FighterChessSkin.FC_Board;

#endregion

namespace FighterChess
{
    /// <summary>
    /// Summary description for form.
    /// </summary>
    public class frmMain : System.Windows.Forms.Form
    {
        private Label lbFighterInfo;

        public frmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            uiBoard = null;
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lbFighterInfo = new System.Windows.Forms.Label();
            // 
            // lbFighterInfo
            // 
            this.lbFighterInfo.Font = new System.Drawing.Font("Tahoma", 16F, System.Drawing.FontStyle.Regular);
            this.lbFighterInfo.ForeColor = System.Drawing.Color.Blue;
            this.lbFighterInfo.Location = new System.Drawing.Point(12, 261);
            this.lbFighterInfo.Size = new System.Drawing.Size(216, 30);
            // 
            // frmMain
            // 
            this.BackColor = System.Drawing.Color.SandyBrown;
            this.ClientSize = new System.Drawing.Size(240, 294);
            this.ControlBox = false;
            this.Controls.Add(this.lbFighterInfo);
            this.MinimizeBox = false;
            this.Text = "Play game";
            this.Load += new System.EventHandler(this.frmMain_Load);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.frmMain_Paint);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseDown);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.frmMain_Closing);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseMove);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmMain_KeyDown);

        }

        #endregion

        private UIBoard uiBoard;
        private Player player1;
        private Player player2;

        #region Properties
        public UIBoard UIBoard
        {
            get { return uiBoard; }
            set { uiBoard = value; }
        }
        #endregion

        public frmMain(Player p1, Player p2)
            : this()
        {
            player1 = new Player(p1.Name, p1.Owner, p1.Active);
            player2 = new Player(p2.Name, p2.Owner, p2.Active);

            //Board board = new Board(player1, player2);
            //uiBoard = new UIBoard(board);
            uiBoard = new UIBoard(player1, player2);
        }

        public frmMain(Board board)
            : this()
        {
            uiBoard = new UIBoard(board);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            uiBoard.Graphic = CreateGraphics();
        }

        private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {   
            if (uiBoard != null && uiBoard.Graphic != null)
                uiBoard.Graphic.Dispose();
        }

        private void frmMain_MouseDown(object sender, MouseEventArgs e)
        {
            // if having fighter doing something => can't do anything
            if (uiBoard.Board.ActiveFighter != null
                && uiBoard.Board.ActiveFighter.Stance != Stance.Stand
                && uiBoard.Board.ActiveFighter.Stance != Stance.Sit)
                return;

            uiBoard.Click(e.X, e.Y);
        }

        private void frmMain_MouseMove(object sender, MouseEventArgs e)
        {
            Square s = uiBoard.GetSquareAt(e.X, e.Y);

            if (s == null)
                return;

            Fighter fighter = uiBoard.Board.GetFighterAt(s.Row, s.Col);
            if (fighter == null)
            {
                if (s.Type == SquareType.LevelUpBlack || s.Type == SquareType.LevelUpWhite)
                    lbFighterInfo.ForeColor = Color.Lime;
                else if (s.Type == SquareType.HpRestoreBlack || s.Type == SquareType.HpRestoreWhite)
                    lbFighterInfo.ForeColor = Color.OrangeRed;
                else if (s.Type == SquareType.Random)
                    lbFighterInfo.ForeColor = Color.Aqua;

                lbFighterInfo.Text = s.Informations;
                return;
            }

            if (fighter.Owner == Owner.White)
                lbFighterInfo.ForeColor = Color.Blue;
            else if (fighter.Owner == Owner.Black)
                lbFighterInfo.ForeColor = Color.Red;

            lbFighterInfo.Text = fighter.Informations;
        }

        private void frmMain_Paint(object sender, PaintEventArgs e)
        {   
            if (uiBoard != null && uiBoard.Board.ActiveFighter == null)
                uiBoard.Draw();
        }

        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Rocker Up
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Rocker Down
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                if (uiBoard.Board.ActiveFighter != null)
                    return; 

                frmOption frm = new frmOption();
                frm.mainForm = this;

                frm.Show();
                this.Hide();
            }

        }
    }
}

